import Taro, { Component } from '@tarojs/taro'
import { View, Text, Image } from '@tarojs/components'
import './personalcenter.css'
import { AtGrid, AtTabBar, AtModal, AtModalHeader, AtModalContent, AtModalAction } from "taro-ui"
import reactMixin from 'react-mixin';
import myJs from './personalcenterJs.js'
import app from '../../../../main.js'

export default class PersonalCenter extends Component {

  config = {
    navigationBarTitleText: '个人中心'
  }

  constructor() {
    super(...arguments);
    this.state = {
      doctor: {},
      tbar_current: 1,
      patient_amount: 0,
      case_amount: 0,
      inquiry_amount: 0,
      isOpenServiceModal: false,
      service_phone: '',
      service_qrcode: '',
    };
  }

  init() { 
    this.getDoctor();
    this.getPatientAmount();
    this.getCaseAmount();
    this.getInquiryAmount();
    this.getServiceInfo();
  }

  getServiceInfo() {
    var me = this;
    var code_phone = '2001';
    var code_qrcoee = '2002';
    app.get('web/configs/list', {codes: [code_phone, code_qrcoee].join(',')}, function(res) {
      var list = res.data.result;
      var service_phone = '';
      var service_qrcode = '';
      for (var i = 0; i < list.length; i++) {
        if (list[i]['code'] == code_phone) {
          service_phone = list[i]['value'];
        }
        if (list[i]['code'] == code_qrcoee) {
          service_qrcode = list[i]['value'];
        }
      }
      me.setState({
        service_phone: service_phone,
        service_qrcode: service_qrcode
      });
    });
  }

  handleClick (value) {
    this.setState({
      tbar_current: value
    })
    if (value == 0) {
      app.redirectTo('../../../doctorpages/homepage/homepage');
    }
    // if (value == 1) {
    //   app.redirectTo('../../../dynamic/pages/dongtaishouye/dongtaishouye');
    // }
  }

  getDoctor() {
    let me = this;
    let data = {};
      app.get("web/doctor/get", data, function(res) {
          console.log("doc",res.data.result)
            me.setState({doctor: res.data.result});
        });
    }

  go(item, index) {
    if (index == 0) {
      app.navigateTo('/pages/center/pages/minecard/minecard'); 
      return;
    }
    if (index == 1) {
      console.log(this.state.doctor.auth_status)
      if(this.state.doctor.auth_status == '审核通过' || this.state.doctor.auth_status == '资质通过' || this.state.doctor.auth_status == '资质不通过' || this.state.doctor.auth_status == '资质审核中'){
          app.navigateTo('../xinxiguanli/xinxiguanli?id='+ this.state.doctor.id)
      }else{
          app.navigateTo('../../../doctorpages/doctorcertified/doctorcertified');  
      }
      return;
    }
    if (index == 2) {
      this.handleServiceModal(true);
      return;
    }
    if(index == 3){
      app.navigateTo('../../pages/mypurse/mypurse')
      return
    }
    if(index == 4){
      app.navigateTo('../../pages/orderrecord/orderrecord')
      return
    }
    if(index == 5){
      app.navigateTo('../../pages/collectlist/collectlist')
    }
    // app.toast('暂未开放，敬请期待');
  }

  getPatientAmount() {
    var me = this;
    app.get('web/statistics/patients', {status: '通过'}, function(res) {
      me.setState({
        patient_amount: res.data.result.count
      });
    });
  }

  getCaseAmount() {
    var me = this;
    app.get('web/statistics/cases', {}, function(res) {
      me.setState({
        case_amount: res.data.result.count
      });
    });
  }

  getInquiryAmount() {
    var me = this;
    app.get('web/statistics/inquiries', {}, function(res) {
      me.setState({
        inquiry_amount: res.data.result.count
      });
    });
  }

  handleServiceModal(v) {
    this.setState({
      isOpenServiceModal: v
    });
  }

  callPhone(phone) {
    Taro.makePhoneCall({
      phoneNumber: phone // 仅为示例，并非真实的电话号码
    })
  }

  render() {
    return (
      <View className='personal-center'>
        <AtModal isOpened={this.state.isOpenServiceModal}>
          <AtModalHeader>联系客服</AtModalHeader>
          <AtModalContent>
            <View className='sv_first'>
              <View className='sv_phone_txt'>联系电话：</View>
              <View className='sv_phone' onClick={this.callPhone.bind(this, this.state.service_phone)}>{this.state.service_phone}</View>
            </View>
            <View className='sv_second'>
              <Image className='sv_qrcode' src={app.getPath(this.state.service_qrcode)} style='width:300rpx;height:300rpx;'></Image>
            </View>
          </AtModalContent>
          <AtModalAction> <Button onClick={this.handleServiceModal.bind(this, false)}>取消</Button></AtModalAction>
        </AtModal>
        <View className='header'>
          <Image className='h-img' src={app.getPath(this.state.doctor.head_image)} onClick={this.go.bind(this,1,1)}></Image>
          <View className='h-first' onClick={this.go.bind(this,1,1)}>{this.state.doctor.name}</View>
          <View className='h-second'>{this.state.doctor.positional_title + ' '}| {this.state.doctor.department}</View>
          <View className='h-third'>{this.state.doctor.hospital}</View>
          <View className='h-last at-row at-row__justify--center more-text'>
            <View className='at-col at-col-2'><Text className='abge'>患者: {this.state.patient_amount}</Text></View>
            <View className='at-col at-col-2'><Text className='abge'>病历: {this.state.case_amount}</Text></View>
            <View className='at-col at-col-2'><Text className='abge'>问诊: {this.state.inquiry_amount}</Text></View>
          </View>
        </View>
        <AtGrid onClick={this.go.bind(this)} className='all' data={
          [
            {
              image: app.imageUri + 'img/wodemingpian.png',
              value: '我的名片'
            },
            {
              image: app.imageUri + 'img/yishirenzheng.png',
              value: `${(this.state.doctor.auth_status == '审核通过' || this.state.doctor.auth_status == '资质通过' || this.state.doctor.auth_status == '资质不通过' || this.state.doctor.auth_status == '资质审核中') ? '医生信息' : '医生认证'}`
            },
            {
              image: app.imageUri + 'img/wodekefu.png',
              value: '联系客服'
            },
            {
              image: app.imageUri + 'img/wodeqianbao.png',
              value: '我的钱包'
            },
            {
              image: app.imageUri + 'img/dingdanjilu.png',
              value: '订单记录'
            },
            {
              image: app.imageUri + 'img/wodeshoucang.png',
              value: '我的收藏'
            }
          ]
        } />
        <AtTabBar
          fixed
          tabList={[
            { title: '首页', iconType: 'home' },
            // { title: '动态', iconType: 'message' },
            { title: '我的', iconType: 'user' }
          ]}
          onClick={this.handleClick.bind(this)}
          current={this.state.tbar_current}
        />
      </View>
    )
  }
}
reactMixin.onClass(PersonalCenter, myJs);
